package com.qf.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

/**
 * @version v1.0.0
 * @Description:
 * @author: 赵英杰
 * @date: 2022.02.14
 * @since 1.0
 */
@Controller
public class DownLoadController {

    @RequestMapping("/download")
    public void download(String imgName, HttpServletResponse response, HttpSession session) throws IOException {
        String realPath = session.getServletContext().getRealPath("/upload");
        if(!new File(realPath).exists()){
            new File(realPath).mkdirs();
        }

        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+imgName);

        IOUtils.copy(new FileInputStream(realPath+"/"+imgName),response.getOutputStream());
    }
}
